package com.javasm.service.stockService.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.javasm.domin.entity.stockEntity.OutBriefList;
import com.javasm.domin.entity.stockEntity.OutDetailedOrder;
import com.javasm.domin.vo.stockVo.OutDetailedInfoVo;
import com.javasm.domin.vo.stockVo.OutDetailedOrderImelVo;
import com.javasm.domin.vo.stockVo.OutDetailedOrderVo;
import com.javasm.mapper.stockMapper.OutBriefListMapper;
import com.javasm.mapper.stockMapper.OutDetailedOrderMapper;
import com.javasm.service.stockService.ExcelService;
import com.javasm.service.stockService.OutBriefListService;
import com.javasm.service.stockService.OutDetailedOrderService;
import com.javasm.service.stockService.OutStorageImelService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author：zzt
 * @Version：1.0
 * @Date：2021/11/23-13:23
 * @Since:jdk1.8
 * @Description:
 */
@Service
@RequiredArgsConstructor
public class ExcelServiceImpl implements ExcelService {
    //库存简表
    private final OutBriefListService outBriefListService;

    //库存详细
    private final OutDetailedOrderService outDetailedOrderService;

    private final OutBriefListMapper outBriefListMapper;

    private final OutDetailedOrderMapper outDetailedOrderMapper;


    @Override
    public void createExcel(HttpServletResponse response,@PathVariable Integer id) {
        String fileName = "销售出库单";
        try {
            fileName = URLEncoder.encode(fileName, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xls");
        OutDetailedInfoVo infoVo = outBriefListService.detailedInfoById(id);
        List<OutDetailedOrderVo> orderVos = outDetailedOrderService.selectDetailedId(id);
        List<OutDetailedOrderImelVo> imelVos = outDetailedOrderService.selectDetailedImelId(id);
        ExcelWriter writer = ExcelUtil.getWriter();
        writer.renameSheet("销售出库订单信息");
        List<String> row1 = CollUtil.newArrayList("销售出库订单号", "订单状态", "业务类型", "销售出库订单时间");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = format.format(infoVo.getSalesOutOrderDate());
        ArrayList<String> list = CollUtil.newArrayList(infoVo.getSalesOutOrderId(), infoVo.getOrderStatus(), infoVo.getBusinessType(), dateString);
        ArrayList<List<String>> lists = CollUtil.newArrayList(row1, list);
        writer.merge(lists.size() - 1, "销售出库订单信息");
        writer.write(lists, true);

        //出库商品列表
        writer.setSheet("出库商品列表");
        ArrayList<OutDetailedOrderVo> outDetailedOrderVos = CollUtil.newArrayList(orderVos);
        writer.addHeaderAlias("goodsId", "商品编号");
        writer.addHeaderAlias("goodsName", "商品名称");
        writer.addHeaderAlias("goodsColor", "商品颜色");
        writer.addHeaderAlias("goodsNum", "订购数量");
        writer.addHeaderAlias("OutImel", "出库串码");
        writer.merge(1, "出库商品列表");
        writer.write(outDetailedOrderVos, true);
        writer.setSheet("出库商品串号列表");
        writer.merge(1, "出库商品串号列表");
        writer.write(imelVos, true);
        ServletOutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        writer.flush(outputStream);
        writer.close();
    }


    //不做评价屎山代码
    @Override
    public void createBatchExcel(HttpServletResponse response, List<String> ids) {
        String fileName = "商品出库单";
        try {
            fileName = URLEncoder.encode(fileName, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xls");

        List<OutBriefList> outBriefLists = outBriefListService.listByIds(ids);
        ArrayList< List<OutDetailedOrderVo>> orderVos = new ArrayList<>();
        ArrayList< List<OutDetailedOrderImelVo>> imels = new ArrayList<>();
        for (OutBriefList outBriefList : outBriefLists) {
            List<OutDetailedOrderVo> vos = outDetailedOrderService.selectDetailedId(outBriefList.getDetailOrderId());
            List<OutDetailedOrderImelVo> imelVos = outDetailedOrderService.selectDetailedImelId(outBriefList.getDetailOrderId());
            orderVos.add(vos);
            imels.add(imelVos);
        }

        ArrayList<OutBriefList> briefLists = CollUtil.newArrayList(outBriefLists);
        ExcelWriter writer = ExcelUtil.getWriter();
        writer.addHeaderAlias("goodsId", "商品编号");
        writer.addHeaderAlias("id", "编号");
        writer.addHeaderAlias("detailOrderId", "详细订单编号");
        writer.addHeaderAlias("goodsName", "商品名称");
        writer.addHeaderAlias("goodsColor", "商品颜色");
        writer.addHeaderAlias("goodsNum", "订购数量");
        writer.addHeaderAlias("salesOutOrderId", "销售出库订单号");
        writer.addHeaderAlias("orderType", "订单类型");
        writer.addHeaderAlias("payType", "支付方式");
        writer.addHeaderAlias("vipAccount", "会员账号");
        writer.addHeaderAlias("orderStatus", "订单状态");
        writer.addHeaderAlias("orderDate", "订单日期");
        writer.addHeaderAlias("noteInfo", "客服备注信息");
        writer.addHeaderAlias("salesOutOrderDate", "销售出库订单时间");
        writer.addHeaderAlias("businessType", "业务类型");
        writer.addHeaderAlias("OutImel", "出库串码");

        writer.renameSheet("销售出库信息");
        writer.merge(1, "销售出库信息");
        writer.write(briefLists, true);

        writer.setSheet("销售出库详细信息");
        writer.merge(1, "销售出库详细信息");
        ArrayList<List<OutDetailedOrderVo>> lists = CollUtil.newArrayList(orderVos);
        for (List<OutDetailedOrderVo> list : lists) {
            writer.write(list, true);
        }

        writer.setSheet("销售出库串号信息");
        writer.merge(1, "销售出库串号信息");
        for (List<OutDetailedOrderImelVo> imel : imels) {
            writer.write(imel, true);
        }

        ServletOutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        writer.flush(outputStream);
        writer.close();
    }



}
